package com.lei.Proxy.statics;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: leiGer
 * @Date: 2021/01/14
 * @Time: 23:01
 */
//手机仓库
public class Depot {
    //单例模式使用的自身实例
    private static Depot depot;
    //仓库
    private static Map<Phone,Integer>reserve;

    //获取仓库实例
    public static synchronized Depot getInstance(){
        if(depot == null){
            depot = new Depot();
        }
        return depot;
    }
    //私有构造方法
    private Depot(){
        reserve = new HashMap<Phone,Integer>();
        //放测试数据
        reserve.put(new MobilePhone("华为","111","红"),3);
        reserve.put(new MobilePhone("小米","22","黑"),5);
    }
    public synchronized static Phone getPhone(String brand,String series,String color){
        Phone phone = new MobilePhone(brand,series,color);
        if(reserve.containsKey(phone)&&reserve.get(phone)>0){
            reserve.put(phone,reserve.get(phone)-1);
            return phone;
        }
        return null;
    }
}
